/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package SQL2003.provider;


import java.util.Collection;
import java.util.List;

import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.edit.provider.ComposeableAdapterFactory;
import org.eclipse.emf.edit.provider.IEditingDomainItemProvider;
import org.eclipse.emf.edit.provider.IItemLabelProvider;
import org.eclipse.emf.edit.provider.IItemPropertyDescriptor;
import org.eclipse.emf.edit.provider.IItemPropertySource;
import org.eclipse.emf.edit.provider.IStructuredItemContentProvider;
import org.eclipse.emf.edit.provider.ITreeItemContentProvider;
import org.eclipse.emf.edit.provider.ItemPropertyDescriptor;
import org.eclipse.emf.edit.provider.ViewerNotification;

import SQL2003.Column;
import SQL2003.ConstructedType;
import SQL2003.DataType;
import SQL2003.Feature;
import SQL2003.Restriction;
import SQL2003.SQL2003Package;

/**
 * This is the item provider adapter for a {@link SQL2003.Column} object.
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
public class ColumnItemProvider
	extends StructuralComponentItemProvider
	implements
		IEditingDomainItemProvider,
		IStructuredItemContentProvider,
		ITreeItemContentProvider,
		IItemLabelProvider,
		IItemPropertySource {
	/**
	 * This constructs an instance from a factory and a notifier.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public ColumnItemProvider(AdapterFactory adapterFactory) {
		super(adapterFactory);
	}

	/**
	 * This returns the property descriptors for the adapted class.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public List<IItemPropertyDescriptor> getPropertyDescriptors(Object object) {
		if (itemPropertyDescriptors == null) {
			super.getPropertyDescriptors(object);

			addDefaultPropertyDescriptor(object);
		}
		return itemPropertyDescriptors;
	}

	/**
	 * This adds a property descriptor for the Default feature.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected void addDefaultPropertyDescriptor(Object object) {
		itemPropertyDescriptors.add
			(createItemPropertyDescriptor
				(((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
				 getResourceLocator(),
				 getString("_UI_Column_default_feature"),
				 getString("_UI_PropertyDescriptor_description", "_UI_Column_default_feature", "_UI_Column_type"),
				 SQL2003Package.Literals.COLUMN__DEFAULT,
				 true,
				 false,
				 false,
				 ItemPropertyDescriptor.GENERIC_VALUE_IMAGE,
				 null,
				 null));
	}

	/**
	 * This returns Column.gif.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object getImage(Object object) {
		return overlayImage(object, getResourceLocator().getImage("full/obj16/Column"));
	}

	/**
	 * This returns the label text for the adapted class.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	@Override
	public String getText(Object object) {
		/*INICIO CODIGO ORIGINAL*/
//		String label = ((Column)object).getName();
//		return label == null || label.length() == 0 ?
//			getString("_UI_Column_type") :
//			getString("_UI_Column_type") + " " + label;
		/*FIN CODIGO ORIGINAL*/

		String name;
		String defaultValue;
		String defaultObject;
		DataType typeObject = null;
		String type = "";
		String features = "";
		String restrictions = "";
		String stereotype = "";
		
		/* Name */
		name = ((Column)object).getName();
		if (name == null || name.length() == 0) {
			return getString("_UI_Column_type");
		}
		
		/* Type */
		typeObject = ((Column)object).getType();
		type = (typeObject == null)? "" : " : " + typeObject.getFriendlyName();

		/* Default Value */
		defaultObject = ((Column)object).getDefault();
		defaultValue = (defaultObject == null || defaultObject.length() == 0)
			? "" : "default=" + defaultObject;
		
		/* Features */
		EList <Feature> featuresList = ((Column)object).getFeatures();
		if (!featuresList.isEmpty()) {
			features += " {";
			for (int i = 0; i<featuresList.size(); i++) {
				features += featuresList.get(i).getFriendlyName();

				if (i < (featuresList.size()-1)) {
					features += ", ";
				}
			}
		}
		if (defaultValue != "") {
			if (featuresList.isEmpty()) {
				defaultValue = " {" + defaultValue;
			}
			else {
				features += ", ";
			}
			features += defaultValue + "}";
		}
		else {
			if (!featuresList.isEmpty()) {
				features += "}";
			}
		}
			
		
		/* Restrictions */
		EList <Restriction> restrictionList = ((Column)object).getRestrictions();	
		if (!restrictionList.isEmpty()) {
			for (int i = 0; i<restrictionList.size(); i++) {
				if (restrictions.indexOf(restrictionList.get(i).getStereotype()) == -1) {
					restrictions += restrictionList.get(i).getStereotype();
					
					if (i < restrictionList.size()-1) {
						restrictions += " ";
					}
				}
			}
		}
		
		/* Stereotype */
		if (type != null) {
			if (typeObject instanceof ConstructedType) {
				stereotype = " " + ((ConstructedType) typeObject).getStereotype();
			}
		}
		
		return getString("_UI_Column_type") + " " + name + type + features + 
			" " + restrictions + stereotype;
	}

	/**
	 * This handles model notifications by calling {@link #updateChildren} to update any cached
	 * children and by creating a viewer notification, which it passes to {@link #fireNotifyChanged}.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void notifyChanged(Notification notification) {
		updateChildren(notification);

		switch (notification.getFeatureID(Column.class)) {
			case SQL2003Package.COLUMN__DEFAULT:
				fireNotifyChanged(new ViewerNotification(notification, notification.getNotifier(), false, true));
				return;
		}
		super.notifyChanged(notification);
	}

	/**
	 * This adds {@link org.eclipse.emf.edit.command.CommandParameter}s describing the children
	 * that can be created under this object.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected void collectNewChildDescriptors(Collection<Object> newChildDescriptors, Object object) {
		super.collectNewChildDescriptors(newChildDescriptors, object);
	}

}
